﻿<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
/********************
 *@param function:OplusCms 前臺 頁面管理
 *@param datetime 2012-01-05
 *@param author solgh
 ********************/
require APPPATH.'/libraries/REST_Controller.php';
class Account extends REST_Controller{
    public function __construct(){
        parent::__construct();
        $this->load->model('admins_mdl');

    }
    /*当天数据*/
    public function today_get(){
        $this->check_user();
        $this->load->model('orders_mdl');
        $today_begin = date("Y-m-d 00:00:00");
        $today_end = date("Y-m-d 23:59:59");
        $today=array(
            'begin_time'=>strtotime($today_begin),
            'end_time'=>strtotime($today_end),
        );
        $today_order=$this->orders_mdl->totle_orders($today);
        $total=$this->db->select_sum('total')->get_where('clickrcms_order','add_time BETWEEN "'.strtotime($today_begin).'" AND "'.strtotime($today_end).'"');
        if(!empty($total) && $total->num_rows()>0){
            $result=$total->row_array();
            $today_total=$result['total'];
        }
        if ($this->member) {
            $order = array(
                'ordernum' => $today_order,
                'ordersum' => $today_total,

            );
        }
        if(isset($order)&&$order){
            $this->response(array('data'=>$order,"status"=>1), 200);
        }else{
            $this->response(array('error' => 'error','status'=>0), 200);
        }
    }
    /*订单列表*/
    public function orders_get(){
        $this->check_user();
        if($this->member){
            $order_status = $this->input->get_post('order_status_id');
            $page = (int)$this->input->get('page');
            $page = $page > 0 ? $page : 1;
            $limits =10;
            $offset =($page-1)*$limits;
            $this->load->model('ordersproduct_mdl');
            if($order_status){
                $map = array('order_status_id'=>$order_status);
                
                if($order_status == 2 || $order_status == 3){
                    $map['add_time >='] = strtotime(date('Y-m-d'),time());
                    $map['add_time <'] = strtotime(date('Y-m-d'),time())+86400;
                }
                $order_list = $this->db->order_by('add_time desc')->limit($limits,$offset)->get_where('clickrcms_order',$map);
                $total = $this->db->get_where('clickrcms_order',$map)->num_rows();
                if(!empty($order_list) && $order_list->num_rows()>0){
                    foreach($order_list->result_array() as $key=>$list){
                        $orders[$key]['order_id'] = $list['id'];
						$orders[$key]['price'] = $list['total'];
                        $orders[$key]['invoicenum'] = $list['invoice_no'];
                        $orders[$key]['nickname'] = $list['nickname'];
                        $orders[$key]['phone'] = $list['phone'];
                        $orders[$key]['address'] = $list['address'];
                        $orders[$key]['create_time'] = date('Y-m-d H:i:s',$list['add_time']);
                        $orders[$key]['order_status']  = $list['order_status_id'];
                        $orders[$key]['goods'] = $this->print_type($list['id']);
                    }

                }
            }else{
                $order_list = $this->db->order_by('add_time desc')->limit($limits,$offset)->get('clickrcms_order');
                $total = $this->db->get('clickrcms_order')->num_rows();
                if(!empty($order_list) && $order_list->num_rows()>0){
                    foreach($order_list->result_array() as $key=>$list){
                        $orders[$key]['order_id'] = $list['id'];
						$orders[$key]['price'] = $list['total'];
                        $orders[$key]['invoicenum'] = $list['invoice_no'];
                        $orders[$key]['nickname'] = $list['nickname'];
                        $orders[$key]['phone'] = $list['phone'];
                        $orders[$key]['address'] = $list['address'];
                        $orders[$key]['create_time'] = date('Y-m-d H:i:s',$list['add_time']);
                        $orders[$key]['order_status']  = $list['order_status_id'];
                        $orders[$key]['goods'] = $this->print_type($list['id']);

                    }

                }

            }

        }
        if(isset($orders)&&$orders){
            $this->response(array('data'=>$orders,"status"=>1,'total'=>$total), 200);
        }else{

            $this->response(array('error' => '暂无訂單','status'=>0,'total'=>$total), 200);
        }

    }

    public function upstatus_get(){
        if($this->member){
            $this->load->model('orders_mdl');
            $order_id = $this->input->get_post('order_id');
            $order_status_id = $this->input->get_post('order_status_id');
            $res = false;
            if(!empty($order_id)&&is_numeric($order_id) && !empty($order_status_id) && $order_status_id==1){
                $res = $this->orders_mdl->edit_orders(array('update_time'=>time(),'order_status_id'=>2),$order_id);
                $this->print_min($order_id);
            }else if(!empty($order_id)&&is_numeric($order_id) && !empty($order_status_id) && $order_status_id==2){
                $res = $this->orders_mdl->edit_orders(array('update_time'=>time(),'order_status_id'=>3),$order_id);
            }else if(!empty($order_id)&&is_numeric($order_id) && !empty($order_status_id) && $order_status_id==4){
                $res = $this->orders_mdl->edit_orders(array('update_time'=>time(),'order_status_id'=>4),$order_id);
            }
            $this->response(array('result'=>$res), 200);
        }
    }
	 public function posprint_get(){

        $order_id = $this->input->get_post('order_id');
        $this->print_min($order_id);
    }
    // private function print_min($order_id){
    //     $this->load->model('orders_mdl');
    //     $this->load->model('ordersproduct_mdl');
    //     $this->load->model('posprint_mdl');
    //     $orderinfo = $this->orders_mdl->get_orders_by_id($order_id);
    //     $goodsinfo = $this->ordersproduct_mdl->_get_order_product($order_id);
    //     if($goodsinfo){
    //         $prints = array();
    //         foreach($goodsinfo as $key=>$list){
    //             $title = $list['pname'];
    //             $specs = explode('_',$list['specs']);
    //             $specArr = $this->_get_posprint($specs);
    //             foreach ($specArr as $spec){
    //                if($spec['is_pos']==1){
    //                     if(!isset($prints[$spec['pos_id']]))
    //                         $prints[$spec['pos_id']] = "<FS2>".$orderinfo['table_num']." </FS2>"."\n\t\t\t\t".date('y/m/d H:i')."\n订单号:".$orderinfo['invoice_no']."\n"."<FS>食品名称</FS>:<FS>".$title."</FS>"."\n*******************************\n";
    //                         $prints[$spec['pos_id']] .= "<FS>".$spec['name']."</FS>";
    //                 }else{
    //                     if(!isset($prints[$spec['pos_id']]))
    //                     $prints[$spec['pos_id']] = "<FS2>".$orderinfo['table_num']." </FS2>"."\n\t\t\t\t".date('y/m/d H:i')."\n订单号:".$orderinfo['invoice_no']."\n*******************************\n";
    //                     $prints[$spec['pos_id']] .= "<FS>".$spec['name']."</FS>";
    //                 }

    //             }
    //         }
    //         $this->load->library('yprint');
    //         foreach ($prints as $k=>$v){
    //             $arr = $this->posprint_mdl->get_posprint_by_id($k);
    //             $this->yprint->action_print($arr['pos_code'],$arr['pos_key'],$v);
    //         }
    //         return true;
    //     }

    //     return false;

    // }
    private function print_min($order_id)
    {
        $this->load->model('orders_mdl');
        $this->load->model('ordersproduct_mdl');
        $this->load->model('posprint_mdl');
        $this->load->library('yprint');
        $orderinfo = $this->orders_mdl->get_orders_by_id($order_id);
        $goodsinfo = $this->ordersproduct_mdl->_get_order_product($order_id);

        if ($goodsinfo) {
            $prints = array();
            $print_content = "<FS2>" . $orderinfo['table_num'] . " </FS2>" . "\n\t\t\t\t" . date('y/m/d H:i') . "\n訂單號:" . $orderinfo['invoice_no'] ;
            $str = '';

            foreach ($goodsinfo as $key => $list) {
                $title = $list['pname'];
                $attrs = key_format_value(!empty($list['attrs']) ? unserialize($list['attrs']) : array());
                $specs = explode('_', $list['specs']);
                if(count($attrs) > 0){
                    $specs = array_merge($specs,array_keys($attrs));
                }
                $str = "\n*********************************************"."\n"."<FS2>"."食品:" . $title ." "."</FS2>";
                $specArr = $this->_get_posprint($specs);
                if(empty($specArr) || count($specArr) <= 0){ //如果没有其他的选项属性，就用产品的打印平台
                    if(!isset($prints[$list['pos_id']])){
                        $prints[$list['pos_id']] = $print_content;
                    }
                    $prints[$list['pos_id']] .= $str."\n";
                    continue;
                }

                $is_title = false;
                foreach ($specArr as $spec) {
                    if($spec['is_pos']==0) continue;
                    if(!isset($prints[$spec['pos_id']])){
                        $prints[$spec['pos_id']] = $print_content;
                        $is_title[$spec['pos_id']] = false;
                    }
                    if(!isset($is_title[$spec['pos_id']]) || !$is_title[$spec['pos_id']]){
                        $prints[$spec['pos_id']] .= $str."\n";
                        $is_title[$spec['pos_id']] = true;
                    }
                    $prints[$spec['pos_id']] .= "<FS2>".$spec['name']."</FS2>".(isset($attrs[$spec['id']]) ? "<FS2>"."x".$attrs[$spec['id']]['num']."</FS2>" : "")." ";
                } 

                for($i=0;$i<$list['quantity'];$i++){
                   foreach ($prints as $k=>$v){
                        $arr = $this->posprint_mdl->get_posprint_by_id($k);
                        $result = $this->yprint->action_print($arr['pos_code'],$arr['pos_key'],$v);
                        //file_put_contents("/prints.log",$result,FILE_APPEND);
                        //$this->yprint->action_print(4004549107,'nvmvi35rzbzm',$v);
                    }
                }
                $prints = array();                  
            }
            return true;
            // $this->load->library('yprint');
            // foreach ($prints as $k=>$v){
            //     $arr = $this->posprint_mdl->get_posprint_by_id($k);
            //     $this->yprint->action_print($arr['pos_code'],$arr['pos_key'],$v);
            // }
            // return true;
        }

        return false;

    }

//    public function t_get(){
//        $order_id = $this->input->get_post('order_id');
//        $this->print_min($order_id);
//        //$prints = $this->print_min($order_id);
//
//    }

    private function print_type($order_id){
        $this->load->model('orders_mdl');
        $this->load->model('ordersproduct_mdl');
        $this->load->model('specs_mdl');
        $goodsinfo = $this->ordersproduct_mdl->_get_order_product($order_id);
        if($goodsinfo){
            $prints = array();
            foreach($goodsinfo as $key=>$list){
                $i=0;
                if(!empty($list['specs'])){
                    $specs = explode('_',$list['specs']);
                    $specArr = $this->_get_posprint($specs);
                    foreach ($specArr as $spec=>$val){
                        $prints[$i]['is_pos'] = $val['is_pos'];
                        $prints[$i]['pos_id'] = $val['pos_id'];
                        $prints[$i]['pos_name'] = $val['name'];
                        $prints[$i]['id'] = $val['id'];
                        $i++;
                    }
                }
                /*多选*/
                if(!empty($list['attrs'])){
                    $attrs = unserialize($list['attrs']);
                    foreach ($attrs as $k => $value) {
                        $spec = $this->specs_mdl->get_spec_byid($value['spec_id']);
                        if(empty($spec)){
                            continue;
                        }
                        $prints[$i]['is_pos'] = $spec['is_pos']; 
                        $prints[$i]['pos_id'] = $spec['pos_id'];
                        $prints[$i]['pos_name'] = $value['name']."+".$value['num'];
                        $prints[$i]['id'] = $value['id'];
                        $i++;
                    }
                }

                $goodsinfo[$key]['specs'] = $prints;
                unset($goodsinfo[$key]['attrs']);
            }

            return $goodsinfo;

        }

        return false;
    }

    protected function _get_posprint($id=array()){
        if(!empty($id)){
            $this->db->where_in('sp.id', $id);
            $this->db->select('s.pos_id,s.is_pos,po.pos_code,po.pos_key,sp.id,sp.name');
            $this->db->from('clickrcms_specs_items AS sp');
            $this->db->join('clickrcms_specs AS s','sp.spec_id = s.id','left');
            $this->db->join('clickrcms_posprint AS po','po.id = s.pos_id','left');
            $this->db->order_by('s.sort ASC');
            $list   = array();
            $result = $this->db->get();
            if($result->num_rows())
                $list = $result->result_array();
            return $list;
        }
    }
    /*android 版本号检查和返回下载路径*/
    public function checkVersionAndroid_get($version=null){
        $this->check_user();
        $version = $this->input->get_post('version');
        if(empty($version)){
            $this->response(array('success' => '','status'=>0,'url'=>''), 200);
        }
        $this->load->model('settings_mdl');
        $result = $this->settings_mdl->get_settings_by_where(array('key'=>'android_version'));
        if(isset($result['value']) && !empty($result['value'])){
            if($version != $result['value']){
                $this->response(array('status'=>1,'url'=>$result['content'],'name'=>$result['value']), 200);
            }
        }
        $this->response(array('success' => '','status'=>0,'url'=>''), 200);
    }
    /*ios版本号检查和下载路径*/
    public function checkVersionIos_get($version=null){
        $this->check_user();
        $version = $this->input->get_post('version');
        if(empty($version)){
            $this->response(array('success' => '','status'=>0,'url'=>''), 200);
        }
        $this->load->model('settings_mdl');
        $result = $this->settings_mdl->get_settings_by_where(array('key'=>'ios_version'));
        if(isset($result['value']) && !empty($result['value'])){
            if($version != $result['value']){
                $this->response(array('status'=>1,'url'=>base_url($result['value'].".ipa")), 200);
            }
        }
    }
    public function check_user(){
        $token = $this->input->get_post('token');
        
        $query = $this->admins_mdl->get_admins_by_token($token);
        if(!$query){
            $this->response(array('error' => 'token is error','status'=>0), 200);
        }
        $this->member = $query; 
    }

}
